<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The MongoDB\Driver\Manager class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="book.mongodb.html">« MongoDB\Driver</a></li>
      <li style="float: right;"><a href="mongodb-driver-manager.addsubscriber.html">MongoDB\Driver\Manager::addSubscriber »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.mongodb.html">MongoDB\Driver</a></li>
    <li>The MongoDB\Driver\Manager class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.mongodb-driver-manager" class="reference">

 <h1 class="title">The MongoDB\Driver\Manager class</h1>
 

 <div class="partintro"><p class="verinfo">(mongodb &gt;=1.0.0)</p>


  <div class="section" id="mongodb-driver-manager.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    The <span class="classname"><strong class="classname">MongoDB\Driver\Manager</strong></span> is the main entry point
    to the extension. It is responsible for maintaining connections to MongoDB
    (be it standalone server, replica set, or sharded cluster).
   </p>
   <p class="para">
    No connection to MongoDB is made upon instantiating the Manager.
    This means the <span class="classname"><strong class="classname">MongoDB\Driver\Manager</strong></span> can always be
    constructed, even though one or more MongoDB servers are down.
   </p>
   <p class="para">
    Any write or query can throw connection exceptions as connections are created lazily.
    A MongoDB server may also become unavailable during the life time of the script.
    It is therefore important that all actions on the Manager to be wrapped in try/catch statements.
   </p>
  </div>


  <div class="section" id="mongodb-driver-manager.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="modifier">final</span>
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">MongoDB\Driver\Manager</strong>
     </span>
     {</div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.addsubscriber.html" class="methodname">addSubscriber</a></span>(<span class="methodparam"><span class="type"><a href="class.mongodb-driver-monitoring-subscriber.html" class="type MongoDB\Driver\Monitoring\Subscriber">MongoDB\Driver\Monitoring\Subscriber</a></span> <code class="parameter">$subscriber</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.construct.html" class="methodname">__construct</a></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$uri</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$uriOptions</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$driverOptions</code><span class="initializer"> = <strong><code>null</code></strong></span></span>)</div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.createclientencryption.html" class="methodname">createClientEncryption</a></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>): <span class="type"><span class="type MongoDB\Driver\ClientEncryption">MongoDB\Driver\ClientEncryption</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.executebulkwrite.html" class="methodname">executeBulkWrite</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$namespace</code></span>, <span class="methodparam"><span class="type"><a href="class.mongodb-driver-bulkwrite.html" class="type MongoDB\Driver\BulkWrite">MongoDB\Driver\BulkWrite</a></span> <code class="parameter">$bulk</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type"><a href="class.mongodb-driver-writeconcern.html" class="type MongoDB\Driver\WriteConcern">MongoDB\Driver\WriteConcern</a></span>|<span class="type">null</span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-writeresult.html" class="type MongoDB\Driver\WriteResult">MongoDB\Driver\WriteResult</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.executecommand.html" class="methodname">executeCommand</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$db</code></span>, <span class="methodparam"><span class="type"><a href="class.mongodb-driver-command.html" class="type MongoDB\Driver\Command">MongoDB\Driver\Command</a></span> <code class="parameter">$command</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type"><a href="class.mongodb-driver-readpreference.html" class="type MongoDB\Driver\ReadPreference">MongoDB\Driver\ReadPreference</a></span>|<span class="type">null</span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-cursor.html" class="type MongoDB\Driver\Cursor">MongoDB\Driver\Cursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.executequery.html" class="methodname">executeQuery</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$namespace</code></span>, <span class="methodparam"><span class="type"><a href="class.mongodb-driver-query.html" class="type MongoDB\Driver\Query">MongoDB\Driver\Query</a></span> <code class="parameter">$query</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type"><a href="class.mongodb-driver-readpreference.html" class="type MongoDB\Driver\ReadPreference">MongoDB\Driver\ReadPreference</a></span>|<span class="type">null</span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-cursor.html" class="type MongoDB\Driver\Cursor">MongoDB\Driver\Cursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.executereadcommand.html" class="methodname">executeReadCommand</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$db</code></span>, <span class="methodparam"><span class="type"><a href="class.mongodb-driver-command.html" class="type MongoDB\Driver\Command">MongoDB\Driver\Command</a></span> <code class="parameter">$command</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-cursor.html" class="type MongoDB\Driver\Cursor">MongoDB\Driver\Cursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.executereadwritecommand.html" class="methodname">executeReadWriteCommand</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$db</code></span>, <span class="methodparam"><span class="type"><a href="class.mongodb-driver-command.html" class="type MongoDB\Driver\Command">MongoDB\Driver\Command</a></span> <code class="parameter">$command</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-cursor.html" class="type MongoDB\Driver\Cursor">MongoDB\Driver\Cursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.executewritecommand.html" class="methodname">executeWriteCommand</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$db</code></span>, <span class="methodparam"><span class="type"><a href="class.mongodb-driver-command.html" class="type MongoDB\Driver\Command">MongoDB\Driver\Command</a></span> <code class="parameter">$command</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-cursor.html" class="type MongoDB\Driver\Cursor">MongoDB\Driver\Cursor</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.getencryptedfieldsmap.html" class="methodname">getEncryptedFieldsMap</a></span>(): <span class="type"><span class="type">array</span>|<span class="type">object</span>|<span class="type">null</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.getreadconcern.html" class="methodname">getReadConcern</a></span>(): <span class="type"><a href="class.mongodb-driver-readconcern.html" class="type MongoDB\Driver\ReadConcern">MongoDB\Driver\ReadConcern</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.getreadpreference.html" class="methodname">getReadPreference</a></span>(): <span class="type"><a href="class.mongodb-driver-readpreference.html" class="type MongoDB\Driver\ReadPreference">MongoDB\Driver\ReadPreference</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.getservers.html" class="methodname">getServers</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.getwriteconcern.html" class="methodname">getWriteConcern</a></span>(): <span class="type"><a href="class.mongodb-driver-writeconcern.html" class="type MongoDB\Driver\WriteConcern">MongoDB\Driver\WriteConcern</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.removesubscriber.html" class="methodname">removeSubscriber</a></span>(<span class="methodparam"><span class="type"><a href="class.mongodb-driver-monitoring-subscriber.html" class="type MongoDB\Driver\Monitoring\Subscriber">MongoDB\Driver\Monitoring\Subscriber</a></span> <code class="parameter">$subscriber</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.selectserver.html" class="methodname">selectServer</a></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="class.mongodb-driver-readpreference.html" class="type MongoDB\Driver\ReadPreference">MongoDB\Driver\ReadPreference</a></span><span class="type"></span></span> <code class="parameter">$readPreference</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.mongodb-driver-server.html" class="type MongoDB\Driver\Server">MongoDB\Driver\Server</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><a href="mongodb-driver-manager.startsession.html" class="methodname">startSession</a></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type MongoDB\Driver\Session">MongoDB\Driver\Session</span></span></div>

   }</div>


  </div>

  <div class="section" id="mongodb-driver-manager.examples">
   <h2 class="title">范例</h2>

   <div class="example" id="example-1294">
    <p><strong>示例 #1 <span class="function"><a href="mongodb-driver-manager.construct.html" class="function">MongoDB\Driver\Manager::__construct()</a></span> basic example</strong></p>
    <div class="example-contents"><p>
     <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span>ing a
     <span class="classname"><strong class="classname">MongoDB\Driver\Manager</strong></span> will print out various
     details about the manager that are otherwise not normally exposed.
     This can be useful to debug how the driver views your MongoDB setup, and 
     which options are used.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$manager&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost:27017'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$manager</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(MongoDB\Driver\Manager)#1 (2) {
  [&quot;uri&quot;]=&gt;
  string(26) &quot;mongodb://127.0.0.1:27017/&quot;
  [&quot;cluster&quot;]=&gt;
  array(0) {
  }
}
</pre></div>
    </div>
   </div>
  </div>
 </div>

 
 













 
















































 






 






 













 






 





















<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="mongodb-driver-manager.addsubscriber.html">MongoDB\Driver\Manager::addSubscriber</a> — Registers a monitoring event subscriber with this Manager</li><li><a href="mongodb-driver-manager.construct.html">MongoDB\Driver\Manager::__construct</a> — Create new MongoDB Manager</li><li><a href="mongodb-driver-manager.createclientencryption.html">MongoDB\Driver\Manager::createClientEncryption</a> — Create a new ClientEncryption object</li><li><a href="mongodb-driver-manager.executebulkwrite.html">MongoDB\Driver\Manager::executeBulkWrite</a> — Execute one or more write operations</li><li><a href="mongodb-driver-manager.executecommand.html">MongoDB\Driver\Manager::executeCommand</a> — Execute a database command</li><li><a href="mongodb-driver-manager.executequery.html">MongoDB\Driver\Manager::executeQuery</a> — Execute a database query</li><li><a href="mongodb-driver-manager.executereadcommand.html">MongoDB\Driver\Manager::executeReadCommand</a> — Execute a database command that reads</li><li><a href="mongodb-driver-manager.executereadwritecommand.html">MongoDB\Driver\Manager::executeReadWriteCommand</a> — Execute a database command that reads and writes</li><li><a href="mongodb-driver-manager.executewritecommand.html">MongoDB\Driver\Manager::executeWriteCommand</a> — Execute a database command that writes</li><li><a href="mongodb-driver-manager.getencryptedfieldsmap.html">MongoDB\Driver\Manager::getEncryptedFieldsMap</a> — Return the encryptedFieldsMap auto encryption option for the Manager</li><li><a href="mongodb-driver-manager.getreadconcern.html">MongoDB\Driver\Manager::getReadConcern</a> — Return the ReadConcern for the Manager</li><li><a href="mongodb-driver-manager.getreadpreference.html">MongoDB\Driver\Manager::getReadPreference</a> — Return the ReadPreference for the Manager</li><li><a href="mongodb-driver-manager.getservers.html">MongoDB\Driver\Manager::getServers</a> — Return the servers to which this manager is connected</li><li><a href="mongodb-driver-manager.getwriteconcern.html">MongoDB\Driver\Manager::getWriteConcern</a> — Return the WriteConcern for the Manager</li><li><a href="mongodb-driver-manager.removesubscriber.html">MongoDB\Driver\Manager::removeSubscriber</a> — Unregisters a monitoring event subscriber with this Manager</li><li><a href="mongodb-driver-manager.selectserver.html">MongoDB\Driver\Manager::selectServer</a> — Select a server matching a read preference</li><li><a href="mongodb-driver-manager.startsession.html">MongoDB\Driver\Manager::startSession</a> — Start a new client session for use with this client</li></ul>
</div>
</div></div></body></html>